package com.itheima.progressbutton_8;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		final ProgressButton progressButton = (ProgressButton) findViewById(R.id.progressButton);
		progressButton.setProgressEnable(true);
		progressButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new AsyncTask<Void, Integer, Void>() {
					int	progress;

					@Override
					protected Void doInBackground(Void... params) {
						while (true) {
							SystemClock.sleep(1000);
							if (progress >= 100) {// 如果当前进度已经大于等于100的时候就跳出while循环
								break;
							}
							progress++;
							publishProgress(progress);
						}
						return null;
					}

					protected void onProgressUpdate(Integer[] values) {
						int progress = values[0];
						progressButton.setProgress(progress);
						progressButton.setText(progress + "%");
					};

				}.execute();
			}
		});
	}

}
